﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class TahasukaSiurKavCard : UserControl
    {     
        TahasukaCard tCard;
        DataTable dt = new DataTable();
        SqlDataAdapter da;
        
        public SqlDataAdapter SqlDataAdapter
        {
            get { return da; }
            set { da = value; }
        }

        public TahasukaSiurKavCard(TahasukaCard tCard)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.tCard = tCard;

            da = new SqlDataAdapter(CompanyManager.getSqlStringFromFile(@"\SELECT\TahasukaSiurKav.SELECT.sql"), CompanyManager.Connection);
            da.UpdateCommand = new SqlCommand(CompanyManager.getSqlStringFromFile(@"\UPDATE\TahasukaSiurKav.UPDATE.sql"),CompanyManager.Connection);
            da.InsertCommand = new SqlCommand(CompanyManager.getSqlStringFromFile(@"\INSERT\TahasukaSiurKav.INSERT.sql"), CompanyManager.Connection);

            IEnumerable<string> sqlParameterCollection = CompanyManager.parseSqlQueryParameters(da.UpdateCommand.CommandText).Distinct<string>();
            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
                switch (item)
                {
                    case "startDate": DateTime dt1 = tCard.StartDate;
                        da.SelectCommand.Parameters.Add(new SqlParameter("@startDate", SqlDbType.Date)).Value = dt1;
                        da.UpdateCommand.Parameters.Add(new SqlParameter("@startDate", SqlDbType.Date)).Value = dt1;
                        da.InsertCommand.Parameters.Add(new SqlParameter("@startDate", SqlDbType.Date)).Value = dt1;
                                      break;

                    default: da.UpdateCommand.Parameters.Add(new SqlParameter("@" + item, SqlDbType.NVarChar));
                        break;

                }

            SqlDataReader reader = da.SelectCommand.ExecuteReader();
            bool isInitialized = reader.Read();

            if (isInitialized)
            {
                textBoxGeneral.Text = reader["general"].ToString();
                textBoxObjective.Text = reader["objective"].ToString();
                textBoxForcesAndMissions.Text = reader["forcesAndMissions"].ToString();
                textBoxRegionHistory.Text = reader["regionalHistory"].ToString();
                textBoxEnemy.Text = reader["enemy"].ToString();
                textBoxSafety.Text = reader["safety"].ToString();
                textBoxLogistics.Text = reader["logistics"].ToString();
                textBoxOther.Text = reader["other"].ToString();
                reader.Close();
            }
            else
            {
                reader.Close();
                da.InsertCommand.ExecuteNonQuery();
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            da.UpdateCommand.Parameters["@general"].Value = textBoxGeneral.Text;
            da.UpdateCommand.Parameters["@objective"].Value = textBoxObjective.Text;
            da.UpdateCommand.Parameters["@forcesAndMissions"].Value = textBoxForcesAndMissions.Text;
            da.UpdateCommand.Parameters["@regionalHistory"].Value = textBoxRegionHistory.Text;
            da.UpdateCommand.Parameters["@enemy"].Value = textBoxEnemy.Text;
            da.UpdateCommand.Parameters["@safety"].Value = textBoxSafety.Text;
            da.UpdateCommand.Parameters["@logistics"].Value = textBoxLogistics.Text;
            da.UpdateCommand.Parameters["@other"].Value = textBoxOther.Text;

            da.UpdateCommand.ExecuteNonQuery();
            labelSuccessfulUpdate.Visible = true;
            labelSuccessfulUpdate.Location = new Point(splitContainer.Panel1.Bounds.Width / 2, 16);
            timer.Interval = 5 * 1000;
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }
    }
}
